package com.turing.manage.computer;

import java.io.IOException;
import java.security.IdentityScope;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings({ "serial", "unused", "deprecation" })
public class ComputerServlet extends HttpServlet {
	
	ComputerService classService = new ComputerService();

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			String method = request.getParameter("method");
			
			if ("query".equals(method))
			{
				this.query(request,response);
			}
			if ("addPage".equals(method))
			{
				this.addPage(request,response);
			}
			if ("add".equals(method))
			{
				this.add(request,response);
			}
			if ("editPage".equals(method))
			{
				this.editPage(request,response);
			}
			if ("edit".equals(method))
			{
				this.edit(request,response);
			}
			if ("delete".equals(method))
			{
				this.delete(request,response);
			}
		} catch (ClassNotFoundException | SQLException e) {
			System.out.println("1.发送的异常信息为:"+e.getMessage());
			System.out.println("2.发送异常的内存地址为:"+e.getStackTrace());
			System.out.println("3.转向到错误处理页面");
			request.setAttribute("errorMsg",e.getMessage());
			request.getRequestDispatcher("/manage/error.jsp").forward(request, response);
		}
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException, ClassNotFoundException, SQLException {
		
			
			  String[] ids = request.getParameterValues("delIdArray");
			  
			  classService.deleteClasss(ids);
			  
			  response.sendRedirect(request.getContextPath()+"/manage/computer.do?method=query");


	}

	private void edit(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, SQLException, IOException {
		
		String computer_id = request.getParameter("computer_id");
		String computer_pinpai = request.getParameter("computer_pinpai");
		String computer_time = request.getParameter("computer_time");
		
		classService.editClasss(computer_id,computer_pinpai,computer_time);
		
		response.sendRedirect(request.getContextPath()+"/manage/computer.do?method=query");
		
	}

	private void editPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ClassNotFoundException, SQLException {
		
		String computer_id = request.getParameter("computer_id");
		
		Map<String,Object> map = classService.editForMap(computer_id);
		
		request.setAttribute("map",map);
		
		request.getRequestDispatcher("/manage/computer/edit.jsp").forward(request, response);
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws IOException, ClassNotFoundException, SQLException {
		
		String computer_pinpai = request.getParameter("computer_pinpai");
		String computer_time = request.getParameter("computer_time");
		
		classService.addClasss(computer_pinpai,computer_time);
		
		response.sendRedirect(request.getContextPath()+"/manage/computer.do?method=query");
	}

	private void addPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/manage/computer/add.jsp").forward(request, response);
	}

	private void query(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, SQLException, ServletException, IOException {
		
		List<Map<String,Object>> list = classService.queryClasssAll();
		
		request.setAttribute("list", list);
		
		request.getRequestDispatcher("/manage/computer/list.jsp").forward(request, response);
		
	}
	
	
}
